#ifndef GAME_ANIMATION_BOID_SHADER_H
#define GAME_ANIMATION_BOID_SHADER_H
//
// Game Animation.
// Copyright (C) 2007 Department of Computer Science, University of Copenhagen
//
#include <animation/configuration.h>

#include <OpenTissue/gpu/cg/cg_program.h>

namespace animation
{

  class BoidShader
  {
  protected:

    OpenTissue::cg::Program                                m_vertex_program;
    OpenTissue::cg::Program                                m_fragment_program;

    bool m_per_pixel_lighting;     ///< Boolean flag used to remember whether per pixel lighting is to be used or not.

  public:

    BoidShader(): m_per_pixel_lighting(false){

	}

  public:
	  void create(bool lreload,bool per_pixel_lighting){
		  bool const reload = per_pixel_lighting != m_per_pixel_lighting;
		  m_per_pixel_lighting = per_pixel_lighting;
		  
		  if(!m_vertex_program.is_program_loaded() || reload||lreload){
			  if(m_per_pixel_lighting){
				  m_vertex_program.load_from_file(OpenTissue::cg::Program::vertex_program,demo_path + "../animation/vp_boid_fragment_lighting.cg");
			  }else{
				  m_vertex_program.load_from_file(OpenTissue::cg::Program::vertex_program,demo_path + "../animation/vp_boid_vertex_lighting.cg");
			  }
		  }
		  if(!m_fragment_program.is_program_loaded() || reload||lreload){
			  if(m_per_pixel_lighting){
				  m_fragment_program.load_from_file(
					  OpenTissue::cg::Program::fragment_program, demo_path + "../animation/fp_boid_fragment_lighting.cg");
			  }else{
				  m_fragment_program.load_from_file(
					  OpenTissue::cg::Program::fragment_program, demo_path + "../animation/fp_boid_vertex_lighting.cg"
				  );
			  }
		  }
	  }
    /**
    * Pre-render.
    *
    * @param per_pixel_lighting  Boolean value indicating whether per pixel or per vertex lighting is to be used on the boid geometry.
    */
    void pre_render (
		float light_x,float light_y,float light_z
		, float intensity
		, float eye_x,float eye_y,float eye_z
		, float fog_density
	    , float fog_color_r, float fog_color_g, float fog_color_b)
    {

      //--- pre-render, setup!
      

      m_vertex_program.set_modelview_projection_matrix();
      m_vertex_program.set_modelview_inverse_transpose();

	  if(!m_per_pixel_lighting){
		  m_vertex_program.set_float_param("light_vec",light_x,light_y,light_z);
		  m_vertex_program.set_float_param("intensity",intensity);
		  m_vertex_program.set_float_param("eye_vec",eye_x,eye_y,eye_z);
		  m_vertex_program.set_float_param( "fog_density",  fog_density  );
		  m_vertex_program.set_float_param( "fog_color",  fog_color_r,fog_color_g,fog_color_b);
	  }else{
		  m_fragment_program.set_float_param("light_vec",light_x,light_y,light_z);
		  m_fragment_program.set_float_param("intensity",intensity);
		  m_fragment_program.set_float_param("eye_vec",eye_x,eye_y,eye_z);
		  m_fragment_program.set_float_param( "fog_density",  fog_density  );
		  m_fragment_program.set_float_param( "fog_color",  fog_color_r,fog_color_g,fog_color_b);
	  }  
	 

	  m_vertex_program.enable();
	  m_fragment_program.enable();
    }

    void post_render()
    {
      m_fragment_program.disable();
      m_vertex_program.disable();
    }

  };

} // namespace animation

// GAME_ANIMATION_BOID_SHADER_H
#endif
